package com.wx.web.controller.user;

import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.eachonline.core.JsonResult;
import com.eachonline.util.RedisHelper;
import com.eachonline.util.UUIDUtil;
import com.wx.business.WXConfig;
import com.wx.business.trade.IxbUserService;
import com.wx.model.IxbUser;
import com.wx.model.IxbUserExample;
import com.wx.web.common.WXHttpUtil;

/**
 * 微信公众号
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/wxuser")
public class WxUserInfoController  {
	
	  public static String strurl="http://admin.ixbonline.com/";
	  
	  public static String adminurl="http://www.ixbonline.com/";;
	
	 @Autowired
	 private IxbUserService ixbUserService;

	@RequestMapping(value = "/auth", method = { RequestMethod.GET })
	@ResponseBody
	public void userGetAuth(HttpServletRequest  request ,HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
        String  redirect_uri=strurl+"token/getCodeToken";
        
        
        response.sendRedirect(WXHttpUtil.requestAuthCode(WXConfig.AppID,URLEncoder.encode(redirect_uri,"UTF-8") , "state"));
        
	}

	
	@RequestMapping(value = "/getInfo", method = { RequestMethod.GET })
	@ResponseBody
	public void userGetToken(HttpServletRequest  request ,String openid,String accessToken,HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        
      
        
        String userJson=WXHttpUtil.requestUserInfo(openid, accessToken);
        JSONObject object=JSONObject.parseObject(userJson);
        
        Map<String,Object> dataMap=new HashMap<String,Object>();
      
        String openId=object.get("openid")+"";
        IxbUserExample userext=new IxbUserExample();
        userext.createCriteria().andOpenidEqualTo(openId);
        List<IxbUser> iuserlist=ixbUserService.selectByExample(userext);
        IxbUser ixbUser=null;
        if(iuserlist!=null&&iuserlist.size()>0)
        {
        	 ixbUser=iuserlist.get(0);
        }
        
        if(ixbUser==null)
        {
        	 IxbUser user=new IxbUser();
             user.setSid(UUIDUtil.getUUID());
             user.setCity(object.get("city")+"");
             user.setCountry(object.get("country")+"");
             user.setCreateTime(new Date());
             user.setOpenid(object.get("openid")+"");
             user.setProvince(object.get("province")+"");
             user.setSex(Integer.valueOf( object.get("sex")+""));
             user.setUserName(object.get("nickname")+"");
             user.setUserPic(object.get("headimgurl")+"");
             ixbUserService.insertSelective(user);
             ixbUser=user;
        }
        // String str="http://192.168.1.140/ixb";
       // String str="http://www.ixbonline.com";
        RedisHelper.setObjectExp(ixbUser.getSid(), ixbUser, 3600*24);
        response.sendRedirect(adminurl+"/index.html?openId="+ixbUser.getOpenid()+"&name="+ixbUser.getUserName()+"&userId="+ixbUser.getSid());
       
	}


	
	
	@RequestMapping(value = "/getUserInfo", method = { RequestMethod.POST })
	@ResponseBody
	public JsonResult getUserInfo(HttpServletRequest  request ,String userId,HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
       
        if(StringUtils.isEmpty(userId))
        {
        	 return  new JsonResult(true,"userId不能为空");
        }
        IxbUser iuser=ixbUserService.selectByPrimaryKey(userId);
        if(iuser==null)
        {
        	 return  new JsonResult(true,"用户信息不存在");
        }
        
        HttpSession  session = request.getSession(true);
		// end
		session.setAttribute("currentAccount", iuser);
        return  new JsonResult(true,iuser);
       
	}

}
